package com.weitu.emergency.controller;


import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.emergency.entity.EmergencySupplies;
import com.weitu.emergency.entity.EmergencySuppliesStock;
import com.weitu.emergency.service.EmergencySuppliesService;
import com.weitu.emergency.service.EmergencySuppliesStockService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-05-02
 */
@Controller
@RequestMapping("emergency/supplies")
public class EmergencySuppliesController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "emergency/supplies";

    private final EmergencySuppliesService emergencySuppliesService;

    private final EmergencySuppliesStockService emergencySuppliesStockService;

    @Autowired
    public EmergencySuppliesController(EmergencySuppliesService emergencySuppliesService, EmergencySuppliesStockService emergencySuppliesStockService) {
        this.emergencySuppliesService = emergencySuppliesService;
        this.emergencySuppliesStockService = emergencySuppliesStockService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listView() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/list");

        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/add");
        return modelAndView;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(String id) {

        EmergencySupplies supplies = emergencySuppliesService.findById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        modelAndView.addObject("supplies", supplies);

        return modelAndView;
    }


    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public ModelAndView detailsView(String id) {

        EmergencySupplies supplies = emergencySuppliesService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/details");
        modelAndView.addObject("supplies", supplies);

        return modelAndView;
    }

    @RequestMapping(value = "/inStock", method = RequestMethod.GET)
    public ModelAndView inStockView(String id) {

        EmergencySupplies supplies = emergencySuppliesService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/inStock");
        modelAndView.addObject("supplies", supplies);

        return modelAndView;
    }

    @RequestMapping(value = "/outStock", method = RequestMethod.GET)
    public ModelAndView outStockView(String id) {

        EmergencySupplies supplies = emergencySuppliesService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/outStock");
        modelAndView.addObject("supplies", supplies);

        return modelAndView;
    }
    @RequestMapping(value = "/inventory", method = RequestMethod.GET)
    public ModelAndView inventoryView(String id) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/inventory");
        modelAndView.addObject("id", id);

        return modelAndView;
    }


    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/findById")
    public EmergencySupplies findById(String id) {
        return emergencySuppliesService.findById(id);
    }

    @ResponseBody
    @RequestMapping("/findByParam")
    public Object findByParam(HttpServletRequest request) {

        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);

        Integer totalNum = emergencySuppliesService.findByParamCount(paramMap);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);

        List<EmergencySupplies> suppliesList = emergencySuppliesService.findByParam(paramMap);

        return pagination.flushDataGridData(suppliesList);
    }

    @ResponseBody
    @RequestMapping("/findStockByParam")
    public Object findStockByParam(HttpServletRequest request) {

        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);

        Integer totalNum = emergencySuppliesStockService.findByParamCount(paramMap);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);

        List<EmergencySuppliesStock> suppliesStockList = emergencySuppliesStockService.findByParam(paramMap);

        return pagination.flushDataGridData(suppliesStockList);
    }



    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @SystemLog(module = "应急资源_应急物资", methods = "添加应急物资")
    public AjaxResult add() {


        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);

        Integer result = emergencySuppliesService.add(paramMap);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @SystemLog(module = "应急资源_应急物资", methods = "编辑应急物资")
    public AjaxResult edit() {

        return updateById();
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @SystemLog(module = "应急资源_应急物资", methods = "删除应急物资")
    public AjaxResult delete() {

        return updateById();
    }

    @ResponseBody
    @RequestMapping(value = "/inStock", method = RequestMethod.POST)
    @SystemLog(module = "应急资源_应急物资", methods = "应急物资入库")
    public AjaxResult inStock() {

        Map<String, Object> stockMap = new HashMap<String, Object>();
        stockMap.put("stockState",0);
        Map<String, Object> suppliesMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(stockMap);
        //获得修改的id
        suppliesMap.put("id",stockMap.get("emergencySuppliesId"));
        suppliesMap.put("inventory",stockMap.get("inventory"));

        Integer result =emergencySuppliesStockService.add(stockMap,suppliesMap);

        return resultAjaxResult(result);

    }

    @ResponseBody
    @RequestMapping(value = "/outStock", method = RequestMethod.POST)
    @SystemLog(module = "应急资源_应急物资", methods = "应急物资出库")
    public AjaxResult outStock() {

        Map<String, Object> stockMap = new HashMap<String, Object>();
        stockMap.put("stockState",1);
        Map<String, Object> suppliesMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(stockMap);
        //获得修改的id
        suppliesMap.put("id",stockMap.get("emergencySuppliesId"));
        suppliesMap.put("inventory",stockMap.get("inventory"));

        Integer result =emergencySuppliesStockService.add(stockMap,suppliesMap);

        return resultAjaxResult(result);

    }

    /**
     * 修改的方法
     *
     * @return 处理结果
     */
    public AjaxResult updateById() {


        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);

        Integer result = emergencySuppliesService.updateById(paramMap);

        return resultAjaxResult(result);
    }


    /**
     * 处理返回参数
     *
     * @param result 返回值
     * @return 返回值
     */
    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }



}
